package it.progettoso.thread;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.HandlerThread;
import android.telephony.TelephonyManager;
import android.telephony.SmsManager;

public class OverviewPhoneThread implements Runnable{
	private TelephonyManager telephonyManager=null;
	private Context c=null;
	private String phone="",mess="",callState="",dataState="",batteryLevel="",posCell="";
	AudioManager manager=null;
	private LocationManager lm;
	private LocationListener locationListener;
	protected double Lat=0.0D,Long=0.0D;
	private HandlerThread mthread;

	
	public OverviewPhoneThread(String phone,Context context,String batteryLevel, HandlerThread mthread) {
		this.phone=phone;
		this.c=context;
		this.batteryLevel=batteryLevel;
		this.mthread=mthread;
		this.telephonyManager=(TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		this.mess="OVERVIEWPHONE:";
		lm = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE); 
		locationListener = new MyLocationListener();
	}
	@Override
	public void run() {
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000,10,locationListener);
	}

	private void getStateInfo() {

		switch (telephonyManager.getCallState()) {
		case TelephonyManager.CALL_STATE_IDLE:
			this.callState="No activity";
			break;
		case TelephonyManager.CALL_STATE_OFFHOOK:
			this.callState="Offhook";
			break;
		case TelephonyManager.CALL_STATE_RINGING:
			this.callState="Ringing";
			break;
		default:
			break;
		}


		switch (telephonyManager.getDataState()) {
		case TelephonyManager.DATA_CONNECTED:
			dataState="Connected";
			break;
		case TelephonyManager.DATA_CONNECTING:
			dataState="Connecting";
			break;		
		case TelephonyManager.DATA_DISCONNECTED:
			dataState="Disconnected";
			break;		
		case TelephonyManager.DATA_SUSPENDED:
			dataState="Suspended";
			break;
		default:
			break;
		}

	}
	private class MyLocationListener implements LocationListener{
		@Override
		public void onLocationChanged(Location loc) {
			if (loc != null) {
				getStateInfo();	
				
				Lat=loc.getLatitude();
				Long=loc.getLongitude();
				
				posCell="Lat:"+loc.getLatitude() + " Long:"+loc.getLongitude();
				
				mess=mess+ "Stato chiamate:"+ callState+ "*Connessione dati:"+ dataState+
				"*Stato Batteria:"+batteryLevel + "*Pos:"+ posCell;
				
				sendSMS(phone, mess);
				mthread.getLooper().quit();

			}
		}
		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {

		}

		@Override
		public void onStatusChanged(String provider, int status,
				Bundle extras) {
		}
	} 
	
	//---sends a SMS message to another device---
	public void sendSMS(String phoneNumber, String message){
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, null, null);
	}  
}
